package com.zkjl.protect_business.utils;

import cn.hutool.core.date.DateException;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil extends cn.hutool.core.date.DateUtil {


    /**
     * 获取指定时间的时间戳
     * @param data yyyy-MM-dd 格式
     * @return
     * @throws ParseException
     */
    public static long getMillis(String data) {


        long timestamp = 0;
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date date = df.parse(data);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            timestamp = cal.getTimeInMillis();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return timestamp;


    }


    /**
     * 获取本周开始时间 时间戳
     * @return
     */
    public static long getWeek() throws ParseException {

        //本周开始时间
        DateTime weekEnd = beginOfWeek(new Date());
        long weekEndNum = DateUtil.getMillis(weekEnd.toDateStr());

        return weekEndNum;


    }


    /**
     * 获取上周开始时间 时间戳
     * @return
     */
    public static long getLastWeek() throws ParseException {
        //上周开始时间
        DateTime weekStart = DateUtil.beginOfWeek(DateUtil.lastWeek());
        long weekStartNum = DateUtil.getMillis(weekStart.toDateStr());
        return weekStartNum;
    }


    /**
     * 是否是今天
     * @param time 毫秒时间戳
     * @return
     */
    public static boolean isToDay(long time) {

        DateTime date = date(time);
        String dateBefore = formatDate(date);
        String today = today();

        if (today.equals(dateBefore)) {
            return true;
        }
        return false;
    }


    /**
     * 是否是本周
     * @return
     */
    public static boolean isWeek(long time) {
        DateTime date = date(time);
        int week = weekOfYear(date);
        int now = weekOfYear(new Date());

        if (week == now) {
            return true;
        }

        return false;

    }

    /**
     * 是否是本月
     * @param time
     * @return
     */
    public static boolean isMonth(long time) {

        DateTime date = date(time);
        String dateBefore = formatDate(date);
        String substringDateBefore = dateBefore.substring(7);

        String today = today();
        String substringAfter = today.substring(7);

        if (substringAfter.equals(substringDateBefore)) {
            return true;
        }
        return false;

    }

    /**
     * 解析月份
     * @param month
     * @return
     */
    public static Long parseMonth(String month) {

        month = normalize(month);
        DateTime parse;
        if (month.contains("-")) {
            parse  = parse(month, "yyyy-MM");
        }else {
            parse = parse(month, "yyyyMM");
        }
        long time = parse.getTime();

        return time;

    }


    /**
     * 格式化时间返回时间戳
     * @param dateStr
     * @return
     */
    public static Long parseDate(String dateStr){
        DateTime parse;
        try {
            parse = parse(dateStr);
        } catch (DateException e) {

            //处理只有年月的情况
            Long aLong = parseMonth(dateStr);
            return aLong;
        }

        return parse.getTime();
    }





    /**
     * 标准化日期，默认处理以空格区分的日期时间格式，空格前为日期，空格后为时间：<br>
     * 将以下字符替换为"-"
     *
     * <pre>
     * "."
     * "/"
     * "年"
     * "月"
     * </pre>
     * <p>
     * 将以下字符去除
     *
     * <pre>
     * "日"
     * </pre>
     * <p>
     * 将以下字符替换为":"
     *
     * <pre>
     * "时"
     * "分"
     * "秒"
     * </pre>
     * <p>
     * 当末位是":"时去除之（不存在毫秒时）
     *
     * @param dateStr 日期时间字符串
     * @return 格式化后的日期字符串
     */
    private static String normalize(CharSequence dateStr) {
        if (StrUtil.isBlank(dateStr)) {
            return StrUtil.str(dateStr);
        }

        // 日期时间分开处理
        final List<String> dateAndTime = StrUtil.splitTrim(dateStr, ' ');
        final int size = dateAndTime.size();
        if (size < 1 || size > 2) {
            // 非可被标准处理的格式
            return StrUtil.str(dateStr);
        }

        final StringBuilder builder = StrUtil.builder();

        // 日期部分（"\"、"/"、"."、"年"、"月"都替换为"-"）
        String datePart = dateAndTime.get(0).replaceAll("[/.年月]", "-");
        datePart = StrUtil.removeSuffix(datePart, "日");
        builder.append(datePart);

        // 时间部分
        if (size == 2) {
            builder.append(' ');
            String timePart = dateAndTime.get(1).replaceAll("[时分秒]", ":");
            timePart = StrUtil.removeSuffix(timePart, ":");
            //将ISO8601中的逗号替换为.
            timePart = timePart.replace(',', '.');
            builder.append(timePart);
        }

        return builder.toString();
    }


}
